from concurrent.futures import ThreadPoolExecutor

from logics.proxy import ProxyLogic
from utils.req_utils import get_session
from configs.configs import HEADERS
from logger import logger

executor = ThreadPoolExecutor(20)

class RefreshProxyJob:

    @staticmethod
    def check_proxy(proxy, url, proxy_logic):
        host, port = proxy.split(':')
        proxyMeta = "http://%(host)s:%(port)s" % {
            "host": host,
            "port": port,
        }

        use_proxy = {
            "http": proxyMeta,
            "https": proxyMeta
        }

        try:
            s = get_session()
            resp = s.get(url, proxies=use_proxy, headers=HEADERS, timeout=5)
            if resp.status_code != 200:
                proxy_logic.delete(proxy)
        except Exception as e:
            logger.warning(f'check proxy error, error: {e}')
            proxy_logic.delete(proxy)

    @staticmethod
    def handle_proxy():
        proxy_logic = ProxyLogic()
        proxies = proxy_logic.all()
        url = "https://www.baidu.com"
        for proxy in proxies:
            executor.submit(RefreshProxyJob.check_proxy, proxy, url, proxy_logic)
        if proxy_logic.count() < proxy_logic.need_num:
            proxy_logic.get_proxies()

    @staticmethod
    def run():
        try:
            RefreshProxyJob.handle_proxy()
        except Exception as e:
            logger.error(f"RefreshProxyJob执行报错, error: {e}")
